#include <DataTypes/DataTypeUUID.h>
#include <DataTypes/DataTypeFactory.h>
#include <DataTypes/Serializations/SerializationUUID.h>
#include <IO/ReadBufferFromString.h>
#include <IO/ReadHelpers.h>


namespace DB
{

bool DataTypeUUID::equals(const IDataType & rhs) const
{
    return typeid(rhs) == typeid(*this);
}

SerializationPtr DataTypeUUID::doGetDefaultSerialization() const
{
    return std::make_shared<SerializationUUID>();
}

Field DataTypeUUID::getDefault() const
{
    return UUID{};
}

MutableColumnPtr DataTypeUUID::createColumn() const
{
    return ColumnVector<UUID>::create();
}

void registerDataTypeUUID(DataTypeFactory & factory)
{
    factory.registerSimpleDataType("UUID", [] { return DataTypePtr(std::make_shared<DataTypeUUID>()); });
}

Field DataTypeUUID::stringToVisitorField(const String & ins) const
{
    // This routine assume ins was generated by FieldVisitorToString
    ReadBufferFromString rb(ins);

    FieldType res;
    readQuoted(res, rb);
    return res;
}

String DataTypeUUID::stringToVisitorString(const String & ins) const
{
    Field field = stringToVisitorField(ins);
    FieldType val = DB::get<FieldType>(field);
    return String(reinterpret_cast<const char *>(&val), getSizeOfValueInMemory());
}

}
